//
//  SizeFiteLabel.m
//  SizeFitLabel
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SizeFiteLabel.h"
@interface SizeFiteLabel()
@property (nonatomic, assign)CGSize expectSize;
@property (nonatomic, strong)UILabel* sizeFitLabel;

@end
@implementation SizeFiteLabel
@synthesize mHV;
@synthesize mFont;
@synthesize mContent;

-(void)setMHV:(BOOL)mHV_
{
    mHV = mHV_;
    
    [self modifyFrame];
}

-(void)setMFont:(UIFont *)mFont_
{
    self.sizeFitLabel.font = mFont_;
    mFont = mFont_;
    
    [self modifyFrame];
}

-(void)setMContent:(NSString *)mContent_
{
    mContent = mContent_;
    self.sizeFitLabel.text = mContent_;
    
    [self modifyFrame];
}

-(void) modifyFrame
{
    if (mContent==nil) {
        return;
    }

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width, 9999);
    if (mHV) {
        maximumLabelSize = CGSizeMake(9999, self.frame.size.height);
    }
    
    self.expectSize = [self.sizeFitLabel sizeThatFits:maximumLabelSize];
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.expectSize.height);
    if (mHV) {
        self.sizeFitLabel.frame = CGRectMake(0, 0, self.expectSize.width, self.frame.size.height);
    }else {
        self.sizeFitLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.expectSize.height);
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.sizeFitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.sizeFitLabel.backgroundColor = [UIColor whiteColor];
        self.sizeFitLabel.numberOfLines = 0;//根据最大行数需求来设置
//        CGSize maximumLabelSize = CGSizeMake(self.frame.size.width, 9999);
//        self.expectSize = [self.sizeFitLabel sizeThatFits:maximumLabelSize];
//        self.sizeFitLabel.frame = CGRectMake(0, 0, self.expectSize.width, self.expectSize.height);
        [self addSubview:self.sizeFitLabel];
    
    }
    return self;
}

- (CGFloat)returnHeight {
    return self.expectSize.width;
}

@end
